﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class RoyalItemDAL : IRoyalItemDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public RoyalItemDAL()
        {
        }
        public IList<RoyalItemInfo> ListRoyalItems(RoyalItemInfo riInfo)
        {
            string strSQL;

            List<RoyalItemInfo> infoList = new List<RoyalItemInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", riInfo.Sequence);

                strSQL = "SELECT ROYAL_ITEM_SEQ, ROYAL_ITEM_CODE, ROYAL_ITEM_NAME, ROYAL_ITEM_NAME_SHORT"
                        + ", ROYAL_ITEM_TYPE_SEQ, RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM HRM_PS_ROYAL_ITEMS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (riInfo.Sequence > 0 ? " AND ROYAL_ITEM_SEQ = @Seq" : "")
                    + " ORDER BY ROYAL_ITEM_CODE";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    RoyalItemTypeDAL typeDAL = new RoyalItemTypeDAL();
                    RoyalItemTypeInfo typeInfo = new RoyalItemTypeInfo();
                    if (!rdr.IsDBNull(4))
                    {
                        IList<RoyalItemTypeInfo> typeList = typeDAL.ListRoyalItemTypes(new RoyalItemTypeInfo(rdr.GetInt32(4)));
                        typeInfo = typeList[0];
                    }

                    RoyalItemInfo Info = new RoyalItemInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)                            
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , typeInfo
                            , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                            , rdr.IsDBNull(6) ? null : (Nullable<int>)rdr.GetInt32(6)
                            , rdr.IsDBNull(7) ? null : (Nullable<int>)rdr.GetInt32(7));
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
        public void Insert(RoyalItemInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@Code", Info.Code);
                dbManager.AddParameters(1, "@Name", Info.Name);
                dbManager.AddParameters(2, "@NameShort", Info.NameShort);
                dbManager.AddParameters(3, "@Type", Info.RoyalItemTypes == null ? (int?)null : Info.RoyalItemTypes.Sequence);
                dbManager.AddParameters(4, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(6, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_ROYAL_ITEMS(ROYAL_ITEM_CODE, ROYAL_ITEM_NAME"
                        + ", ROYAL_ITEM_NAME_SHORT, ROYAL_ITEM_TYPE_SEQ"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@Code, @Name, @NameShort, @Type, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(RoyalItemInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@Code", Info.Code);
                dbManager.AddParameters(1, "@Name", Info.Name);
                dbManager.AddParameters(2, "@NameShort", Info.NameShort);
                dbManager.AddParameters(3, "@Type", Info.RoyalItemTypes == null ? (int?)null : Info.RoyalItemTypes.Sequence);
                dbManager.AddParameters(4, "@Sequence", Info.Sequence);
                dbManager.AddParameters(5, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(6, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEMS"
                           + " SET ROYAL_ITEM_CODE = @Code"
                                + ", ROYAL_ITEM_NAME = @Name"
                                + ", ROYAL_ITEM_NAME_SHORT = @NameShort"
                                + ", ROYAL_ITEM_TYPE_SEQ = @Type"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE ROYAL_ITEM_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEMS"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ROYAL_ITEM_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }        
    }
}
